<?php

namespace app\model;

use app\controller\wmapi\IndexController;
use support\Db;
use support\Model;
use PDO;

class DcdSpecsCans
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'dcd_specs_cans';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * dcd车型详情列表 - 分页
     * @return false|string
     */
    public static function list($page, $pageNum, $fields, $condition)
    {
        $offset = ($page - 1) * $pageNum;
        $model = Db::table('dcd_specs_cans')
            ->select($fields)
            ->where($condition);
        $count = $model->where($condition)->count();
        $list = $model
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);
        if ($list) {
            foreach ($list as $k) {
                if ($k){
                    if ($k->results_data){
//                    $k->results_data = json_decode($k->results_data);
                        $k->results_data = [];
                    }
                    if ($k->official_price){
                        $officce = explode('万', $k->official_price);
                        if ($officce[0] == '暂无报价'){

                            $k->official_price = 0.00;

                        }else{
                            $k->official_price = round($officce[0], 2) * 10000;
                        }
                    }else{
                        $k->official_price = 0.00;
                    }


                }
            }
        }

        return json_encode(['list' => $list, 'count' => $count]);
    }


    /**
     * dcd车型详情列表 - 无分页
     * @param $page
     * @param $pageNum
     * @param $fields
     * @param $condition
     * @return false|string
     */
    public static function all($fields, $condition)
    {

        $list = Db::table('dcd_specs_cans')
            ->select($fields)
            ->where($condition)
            ->get();
        if ($list){
            foreach ($list as $k){
                if ($k){
                    if ($k->is_nev){
                        $k->is_nev = intval($k->is_nev);
                    }
                    if ($k->is_nev == 1){
                        $k->is_nev_name = '新能源';
                    }elseif ($k->is_nev == 2){
                        $k->is_nev_name = '传统能源';
                    }else{
                        $k->is_nev_name = '';
                    }


//                    if ($k->results_data){
//                    $k->results_data = json_decode($k->results_data);
//                        $k->results_data = [];
//                    }
                    if ($k->official_price){
                        $officce = explode('万', $k->official_price);
                        if ($officce[0] == '暂无报价'){

                            $k->official_price = 0.00;

                        }else{
                            $k->official_price = round($officce[0], 2) * 10000;
                        }
                    }else{
                        $k->official_price = 0.00;
                    }






                    // 获取当前这个产品的所有报价单中 行情报价的数据并算出 最低 和 最高 的报价
                    $times = time() - (60*60*24*30);
                    $start_time = date('Y-m-d H:i:s', $times);
                    $condition_purchase_quotation = [];
                    $condition_purchase_quotation [] = ['model', 'like', '%' . $k->car_name . '%'];
                    $condition_purchase_quotation [] = ['created_at', '>', $start_time];

                    $condition_feild = ['count','purchase_quotation','created_at'];
                    $ary = [];
                    $count = 0;
                    $purchase_quotation = 0;
                    $quotationOrderChildrenByPurchaseQuotation = WaOrdersQuotationRelevance::allByPurchaseQuotation($condition_feild,$condition_purchase_quotation);
                    $quotationOrderChildrenByPurchaseQuotation = json_decode($quotationOrderChildrenByPurchaseQuotation);


                    if ($quotationOrderChildrenByPurchaseQuotation){
                        foreach ($quotationOrderChildrenByPurchaseQuotation as $kq){
                            $count = $count + round($kq->count,2);
                            $purchase_quotation = $purchase_quotation + round($kq->purchase_quotation,2);
                            array_push($ary,round($kq->purchase_quotation,2));
                        }
                        $k->min_purchase_quotation = reset($ary);
                        $k->average_purchase_quotation = round($purchase_quotation/$count,2);
                        $k->max_purchase_quotation = end($ary);
                    }else{
                        $k->min_purchase_quotation = '暂无';
                        $k->average_purchase_quotation = '暂无';
                        $k->max_purchase_quotation = '暂无';
                    }
                    $k->count_purchase_quotation = $count;
//                    $rule = json_encode($rule);





                }
            }
        }



        return json_encode($list);
    }

    public static function allWhereIn($fields, $condition,$whereIn)
    {

        $list = Db::table('dcd_specs_cans')
            ->select($fields)
            ->where($condition)
            ->whereIn('car_id',[100195,97955,97126,87128,77251,93540,76433,29570,241330,101199,97402,94418,94765])
            ->get();
        if ($list){
            foreach ($list as $k){
                if ($k){
                    $k->is_nev = intval($k->is_nev);
                    if ($k->is_nev == 1){
                        $k->is_nev_name = '新能源';
                    }elseif ($k->is_nev == 2){
                        $k->is_nev_name = '传统能源';
                    }else{
                        $k->is_nev_name = '';
                    }


                    if ($k->results_data){
//                    $k->results_data = json_decode($k->results_data);
//                        $k->results_data = [];
                    }
                    if ($k->official_price){
                        $officce = explode('万', $k->official_price);
                        if ($officce[0] == '暂无报价'){

                            $k->official_price = 0.00;

                        }else{
                            $k->official_price = round($officce[0], 2) * 10000;
                        }
                    }else{
                        $k->official_price = 0.00;
                    }


                }
            }
        }



        return json_encode($list);
    }

    public static function allBrandByDcdBrand($fields, $condition)
    {
        $list = Db::table('dcd_brands')
//        $list = Db::table('dcd_brands')
            ->select($fields)
            ->where($condition)
            ->get();

        return json_encode($list);
    }


    public static function allBrandBySpecs($fields, $condition)
    {
        $list = Db::table('dcd_specs_cans')
//        $list = Db::table('dcd_brands')
            ->select('brand_name')
            ->where($condition)
            ->get();

        if ($list){
            $list =  array_unique(array_column(json_decode($list),'brand_name'));
        }

        return json_encode($list);
    }



    public static function allBrandSub($fields, $condition)
    {

        $list = Db::table('dcd_specs_cans')
            ->select('sub_brand_name')
            ->where($condition)
            ->get();

        if ($list){
            $list =  array_unique(array_column(json_decode($list),'sub_brand_name'));

        }
        return json_encode($list);
    }

    public static function allSeries($fields, $condition)
    {

        $list = Db::table('dcd_specs_cans')
            ->select('series_name')
            ->where($condition)
            ->get();
        if ($list){
            $list =  array_unique(array_column(json_decode($list),'series_name'));
        }

        return json_encode($list);
    }

    public static function allModel($fields, $condition)
    {

        $list = Db::table('dcd_specs_cans')
            ->select('car_name')
            ->where($condition)
            ->get();
        if ($list){
            $list =  array_unique(array_column(json_decode($list),'car_name'));
        }
        return json_encode($list);
    }


    public static function allModelYear($fields, $condition)
    {

        $list = Db::table('dcd_specs_cans')
            ->select('car_year')
            ->where($condition)
            ->get();
        if ($list){
            $list =  array_unique(array_column(json_decode($list),'car_year'));
        }
        return json_encode($list);
    }

    public static function find($condition)
    {
        $res = Db::table('dcd_specs_cans')
            ->select('*')
            ->where($condition)
            ->first();
        if ($res) {
            if ($res->results_data) {
                $res->results_data = json_decode($res->results_data);
            }


            if ($res->official_price){
                $officce = explode('万', $res->official_price);
                if ($officce[0] == '暂无报价'){

                    $res->official_price = 0.00;

                }else{
                    $res->official_price = round($officce[0], 2) * 10000;
                }
            }else{
                $res->official_price = 0.00;
            }
            $res->is_nev = intval($res->is_nev);
            if ($res->is_nev == 1){
                $res->is_nev_name = '新能源';
            }elseif ($res->is_nev == 2){
                $res->is_nev_name = '传统能源';
            }else{
                $res->is_nev_name = '';
            }


        }
        return json_encode($res);
    }

    public static function created($data)
    {

        return Db::table('dcd_specs_cans')->insertGetId($data);

    }

    public static function updated($condition, $data)
    {

        $res = Db::table('dcd_specs_cans')->where($condition)->update($data);
        return json_encode($res);

    }

    public static function deleted($condition)
    {

        $res = Db::table('dcd_specs_cans')->where($condition)->delete();
        return json_encode($res);

    }


    public static function updatedReserveBbrands($condition, $data)
    {

        $res = Db::table('dcd_specs_cans')->whereNotIn('brand_name',$condition)->update($data);
        return json_encode($res);

    }

    public static function showColumns($column,$type)
    {




        $res = 1;
        return json_encode($res);

    }







}